package dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;

import commonTypes.Bill;
import commonTypes.Dish;
import commonTypes.Ordered;
import commonTypes.OrderedStatus;

public class OrderedDaoImpl implements OrderedDao{
	private JdbcTemplate jdbcTemplate;
	
	public void setJdbcTemplate(JdbcTemplate jdbcTemplate){
		this.jdbcTemplate = jdbcTemplate;
	}
	public void createOrdered(Ordered ordered) {
		String idString = this.createUUID();
		String sqlStr = "INSERT INTO ordered (TableID ,DishID ,Time,OrderID,DishName)" +
		"VALUES ('" + ordered.getTableId() + "','" + ordered.getDishId() + "', '" + ordered.getTime() + "','"+idString+ "', '" + ordered.getDishName()+"');";
		jdbcTemplate.update(sqlStr);
	}

	public void deleteOrdered(int tableID, int dishID) {
		String sqlStr = "DELETE FROM ordered WHERE TableID = ? AND DishID = ?";
		jdbcTemplate.update(sqlStr, new Object[]{tableID,dishID});
	}
	
	public void deleteOrderedByTableID(int tableID) {
		String sqlStr = "DELETE FROM ordered WHERE TableID = ?";
		jdbcTemplate.update(sqlStr, new Object[]{tableID});
	}
	public void deleteOrderedByOrderedID(String orderedId) {
		String sqlStr = "DELETE FROM ordered WHERE OrderId = ?";
		jdbcTemplate.update(sqlStr, new Object[]{orderedId});
	}
	
	public List<Ordered> getAllOrdered() {
		String sqlStr = "SELECT * FROM ordered";
		final List<Ordered> orderedList =new ArrayList<Ordered>();
		jdbcTemplate.query(sqlStr, new RowCallbackHandler(){
			public void processRow(ResultSet rs) throws SQLException {
				while(!rs.isAfterLast()) {
					Ordered ordered = new Ordered();
					ordered.setDishId(rs.getInt("DishID"));
					ordered.setDishName(rs.getString("DishName"));
					ordered.setOrderedId(rs.getString("OrderID"));
					int orderedStatus = OrderedStatus.WAITING;
					if(rs.getInt("OrderedStatus") == 0){
						orderedStatus = OrderedStatus.WAITING;
					}else if(rs.getInt("OrderedStatus") == 1){
						orderedStatus = OrderedStatus.DOING;
					}else if(rs.getInt("OrderedStatus")==2){
						orderedStatus = OrderedStatus.DONE;
					}
					ordered.setOrderedStatus(orderedStatus);
					ordered.setTableId(rs.getInt("TableID"));
					ordered.setTime(rs.getLong("Time"));
					if(orderedStatus != OrderedStatus.WAITING){
						ordered.setKitchenId(rs.getInt("KitchenID"));
					}
					orderedList.add(ordered);
					rs.next();
				}
			}
		});
		return orderedList;
	}
	
	public Ordered getOrderedById(String orderedId){
		String sqlStr = "SELECT * FROM ordered WHERE OrderID = '"+orderedId+"'";
		final Ordered ordered = new Ordered();
		jdbcTemplate.query(sqlStr, new RowCallbackHandler(){
			public void processRow(ResultSet rs) throws SQLException {
				ordered.setDishId(rs.getInt("DishID"));
				ordered.setDishName(rs.getString("DishName"));
				ordered.setOrderedId(rs.getString("OrderID"));
				int orderedStatus = OrderedStatus.WAITING;
				if(rs.getInt("OrderedStatus") == 0){
					orderedStatus = OrderedStatus.WAITING;
				}else if(rs.getInt("OrderedStatus") == 1){
					orderedStatus = OrderedStatus.DOING;
				}else if(rs.getInt("OrderedStatus")==2){
					orderedStatus = OrderedStatus.DONE;
				}
				ordered.setOrderedStatus(orderedStatus);
				ordered.setTableId(rs.getInt("TableID"));
				ordered.setTime(rs.getLong("Time"));
				if(orderedStatus != OrderedStatus.WAITING){
					ordered.setKitchenId(rs.getInt("KitchenID"));
				}
			}
		});
		return ordered;
	}
	
	public void setOrdered(Ordered ordered){
		String sqlStr = "UPDATE ordered SET TableID=?,DishID=?,Time=?, DishName=?, OrderedStatus=?,KitchenID=? WHERE OrderID=?";
		jdbcTemplate.update(sqlStr,new Object[] {ordered.getTableId(),ordered.getDishId(),ordered.getTime(),ordered.getDishName(),ordered.getOrderedStatus(),ordered.getKitchenId(),ordered.getOrderedId()});
	}
	
	private String createUUID(){
		UUID uuid = UUID.randomUUID();
		String uuidString = uuid.toString();
		String[] uuidStringArray = uuidString.split("-");
		String idString="";
		for(String str:uuidStringArray){
			idString += str;
		}
		return idString;
	}

	public List<Ordered> getOrderedByDishIDAndTableID(int dishId, int tableId) {
		String sqlStr = "SELECT * FROM ordered WHERE DishID = "+dishId+" AND TableID="+tableId;
		final List<Ordered> orderedList =new ArrayList<Ordered>();
		jdbcTemplate.query(sqlStr, new RowCallbackHandler(){
			public void processRow(ResultSet rs) throws SQLException {
				while(!rs.isAfterLast()) {
					Ordered ordered = new Ordered();
					ordered.setDishId(rs.getInt("DishID"));
					ordered.setDishName(rs.getString("DishName"));
					ordered.setOrderedId(rs.getString("OrderID"));
					int orderedStatus = OrderedStatus.WAITING;
					if(rs.getInt("OrderedStatus") == 0){
						orderedStatus = OrderedStatus.WAITING;
					}else if(rs.getInt("OrderedStatus") == 1){
						orderedStatus = OrderedStatus.DOING;
					}else if(rs.getInt("OrderedStatus")==2){
						orderedStatus = OrderedStatus.DONE;
					}
					ordered.setOrderedStatus(orderedStatus);
					ordered.setTableId(rs.getInt("TableID"));
					ordered.setTime(rs.getLong("Time"));
					if(orderedStatus != OrderedStatus.WAITING){
						ordered.setKitchenId(rs.getInt("KitchenID"));
					}
					orderedList.add(ordered);
					rs.next();
				}
			}
		});
		return orderedList;
	}
	
	public void createEndedOrdered(Ordered ordered){
		String sqlStr = "INSERT INTO ordered_ended (OrderedID ,DishID,TableID ,Time,DishName)" +
		"VALUES ('" + ordered.getOrderedId() + "','" + ordered.getDishId() +"', '" + ordered.getTableId() + "', '" + ordered.getTime() + "', '" + ordered.getDishName()+"');";
		jdbcTemplate.update(sqlStr);
	}

}
